package com.lxp.project.system.service.impl;

import com.lxp.project.system.domain.SysRoleMenu;
import com.lxp.project.system.mapper.SysRoleMenuMapper;
import com.lxp.project.system.service.ISysRoleMenuService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {
    @Override
    public long checkMenuExistRole(Long menuId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(SysRoleMenu::getMenuId, menuId);
        return this.count(wrapper);
    }

    @Override
    public int deleteRoleMenuByRoleId(Long roleId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(SysRoleMenu::getRoleId, roleId);
        return this.mapper.deleteByQuery(wrapper);
    }

    @Override
    public int deleteRoleMenu(Long[] roleIds) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in(SysRoleMenu::getRoleId, Arrays.asList(roleIds));
        return this.mapper.deleteByQuery(wrapper);
    }

    @Transactional
    @Override
    public int batchRoleMenu(List<SysRoleMenu> roleMenuList) {
        return this.saveBatch(roleMenuList) ? roleMenuList.size() : 0;
    }
}
